package com.suse.springbook.service;


import com.suse.springbook.enums.StatusEnum;
import com.suse.springbook.mapper.BookMapper;
import com.suse.springbook.model.BookInfo;
import com.suse.springbook.model.PageResult;
import com.suse.springbook.model.RequestPage;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
//    @Autowired
//    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;

//    public List<BookInfo> getList() {
//
//        List<BookInfo> bookInfos = bookDao.mookData();
//        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStateCN("可借阅");
//            } else {
//                bookInfo.setStateCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public Integer addBook(BookInfo bookInfo) {
       return  bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        //1.count
        Integer count = bookMapper.count();
//        int offset = requestPage.getOffset();
        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(),requestPage.getPageSize());

        for(BookInfo bookInfo : bookInfos) {
            bookInfo.setStateCN(StatusEnum.getNameByCode((bookInfo.getStatus())).getName());
        }

        return new PageResult<>(bookInfos,count,requestPage);

//        PageResult<BookInfo> result = new PageResult<>();
//        result.setItems(bookInfos);
//        result.setCount(count);
//        return result;
    }


    public BookInfo getBookInfo(Integer bookId) {
        BookInfo bookInfo = bookMapper.getBookInfo(bookId);
        return bookInfo;
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {

        return bookMapper.batchDelete(ids);
    }
}
